<?php

namespace app\api1\controller;

use hg\apidoc\annotation as Apidoc;
use think\facade\Db;

class RobotProducts extends Auth
{
    /**
     * list
     * @Apidoc\Title("机器人产品列表")
     */
    public function list()
    {
        $robot_price = 0;
        $robot_id = input('robot_id', 0, 'intval');
        if($robot_id){
            $robot_price = \app\common\model\Robot::where('id', $robot_id)->value('daily_price');
        }
        $count = Db::name('robot')->where('member_id', $this->request->uid)->count();
        $data = \app\common\model\RobotProducts::order('sort', 'asc')->where('is_open',1)->select();
        $data->each(function ($item) use ($count, $robot_price) {
            if ($item->price == 0 && $count > 0) {
                $item->can_new = false;
            } else {
                $item->can_new = true;
            }
            // 产品每天价格和$robot_price 对比
            $daly_price = $item->price / $item->duration_days;
            $diff_price = $daly_price - $robot_price;
            $item->diff_price = $diff_price;
            if($diff_price > 0) {
                $item->can_update = true;
            }else{
                $item->can_update = false;
            }
            $item->items = explode("\n", trim($item->functional_content));

        });
        return success('', $data);
    }

}